package com.golaberto.client;

import com.golaberto.client.FilteredGrid.ListItemsCallback;

import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

import java.util.ArrayList;

public class Championships extends WorkPanel
    implements ListItemsCallback, ClickListener {
  private FilteredGrid grid_ = new FilteredGrid(20, 1, false);
  private Button load_ = new Button("Load", this);
  private Button del_ = new Button("Delete", this);
  private Button new_ = new Button("New", this);

  public static WorkPanelInfo init() {
    return new WorkPanelInfo("Championships", "Championships") {
      public WorkPanel createInstance() {
        return new Championships();
      }
    };
  }

  public Championships() {
    final DockPanel panel = new DockPanel();
    panel.setWidth("100%");

    grid_.setWidth("100%");
    grid_.setListItemsCallback(this);
    grid_.updateGrid();
    panel.add(grid_, DockPanel.CENTER);

    HorizontalPanel bottom_panel = new HorizontalPanel();
    bottom_panel.add(load_);
    bottom_panel.add(del_);
    bottom_panel.add(new_);

    panel.add(bottom_panel, DockPanel.SOUTH);
    initWidget(panel);
  }

  public void onClick(Widget sender) {
    if (sender == load_) {
    } else if (sender == del_) {
    } else if (sender == new_) {
    }
  }

  public void getItems(String text, int page, int count) {
    if (!HTTPRequest.asyncGet(
             "current_championships.php?text=" + text + "&page=" + page + "&count=" + count,
             new JSONResponseTextHandler())) {
      Window.alert("Failed");
    }      
  }

  private class JSONResponseTextHandler implements ResponseTextHandler {
    public void onCompletion(String responseText) {
      //grid_.addItem("Completed");
      try {
        JSONValue jsonValue = JSONParser.parse(responseText);
        displayJSONObject(jsonValue);
      } catch (JSONException e) {
        displayError(responseText);
      }
    }
  }

  private void displayError(String responseText) {
    Window.alert(responseText);
  }

  private void displayJSONObject(JSONValue jsonValue) {
    JSONArray jsonArray;
    if ((jsonArray = jsonValue.isArray()) != null) {
      ArrayList matches = new ArrayList();
      for (int i = 0; i < jsonArray.size(); ++i) {
        JSONArray object = jsonArray.get(i).isArray();
        String name = object.get(1).isString().stringValue();
        String begin = object.get(2).isString().stringValue().substring(0, 4);
        String end = object.get(3).isString().stringValue().substring(0, 4);
        String suffix;
        if (begin.equals(end)) {
          suffix = begin;
        } else {
          suffix = begin.concat("/").concat(end);
        }
        
        matches.add(name.concat(" ").concat(suffix));
      }
      String[][] items = new String[matches.size()][1];
      for (int i = 0; i < matches.size(); ++i) {
        items[i][0] = (String)matches.get(i);
      }
      grid_.onGetItemsDone(items);
    }
  }

  public void onShow() {
  }
}
